﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
using DunLibrary.Client;
using DunSilverlight.Comm;
using DunLibrary.Query;

namespace DunSilverlight.Views
{
    public partial class ImportReturnRecord : UserControl
    {
        public ImportReturnRecord()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(ImportReturnRecord_Loaded);
        }

        void ImportReturnRecord_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                Action<IList<Company>> getBanks = (IList<Company> banks) =>
                {
                    cmbBank.ItemsSource = banks;
                };

                AppGlobal.GetBanks(getBanks);

            }
            catch (Exception ex)
            {
                Tools.ShowMessage(ex.Message, "", false);
            }
        }
        /// <summary>
        /// 文件上传完成后开始执行导入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RadUpload1_FileUploaded(object sender, Telerik.Windows.Controls.FileUploadedEventArgs e)
        {
            try
            {
                if (CheckImport())
                {
                    RadUploadSelectedFile files = e.SelectedFile;

                    string fileName = files.Name;

                    ImportService.ImportServiceClient ser = new ImportService.ImportServiceClient();

                    ser.ImportReturnRecordsCompleted += (object sender2, ImportService.ImportReturnRecordsCompletedEventArgs e2) =>
                    {
                        string result = e2.Result;
                        JsonResult r = null;
                        try
                        {
                            r = Newtonsoft.Json.JsonConvert.DeserializeObject<JsonResult>(result);
                            Loger.WriteLog("导入", "导入回款,文件名:" + fileName);
                        }
                        catch (Exception ex)
                        {
                            Tools.ShowMessage(ex.Message, "", false);
                        }
                        MessageBox.Show(r.Data + r.Message);
                    };

                    int bankId = ((Company)cmbBank.SelectedItem).Id;

                    DunLibrary.User.User user = AppGlobal.CurrentUser;

                    ser.ImportReturnRecordsAsync(RadUpload1.TargetFolder + @"\" + fileName, bankId,user.Organization.Id, user.Id);
                }

            }
            catch (Exception ex)
            {
                Tools.ShowMessage(ex.Message, "", false);
            }
        }

        private bool CheckImport()
        {

            object bank = cmbBank.SelectedItem;

            if (bank == null)
            {
                MessageBox.Show("请选择银行!");

                return false;
            }

            return true;
        }


        private void RadUpload1_UploadStarted(object sender, Telerik.Windows.Controls.UploadStartedEventArgs e)
        {

        }
    }
}
